/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.risk.util;

import com.koron.audit.domain.Audit;
import com.koron.common.bean.StaffBean;
import com.koron.common.web.Util;
import com.koron.risk.bean.DeptSafetyStaffBean;
import com.koron.risk.domain.GridNode;
import com.koron.risk.mapper.GridNodeMapper;
import com.koron.risk.service.api.GridNodeService;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class GridHelpUtils {

    @Autowired
    private GridNodeService nodeService;

    @Autowired
    private Util util;

    //查找给定网格code的负责人,返回网格负责人的userid
    public String getGridManager(String gridNode, Audit audit) {
        try (SessionFactory factory = new SessionFactory()) {
            GridNode node = factory.getMapper(GridNodeMapper.class).queryByCode(gridNode);
//            StringBuilder gridManager = new StringBuilder();
            List<String> userIds = new ArrayList<>();
            if (null != node) {
                if (StringUtils.isEmpty(node.getSafetyOfficer())) {
                    if (StringUtils.isNotEmpty(node.getSafetyLeader())) {
                        String[] userCodes = node.getSafetyLeader().split("[,，]");
                        if (null != userCodes && userCodes.length > 0) {
                            for (String userCode : userCodes) {
                                List<StaffBean> staffBeanList = (List<StaffBean>) util.staff(userCode, null).getData();
                                if (null != staffBeanList && staffBeanList.size() > 0) {
                                    userIds.add(staffBeanList.get(0).getUserid());
                                }
                            }
                        }
//                    } else {
//                        //去找离得最近的网格节点的相关人
//                        setGridManager(userIds, gridNode, audit);
                    }
                } else {
                    String[] userCodes = node.getSafetyOfficer().split("[,，]");
                    if (null != userCodes && userCodes.length > 0) {
                        for (String userCode : userCodes) {
                            List<StaffBean> staffBeanList = (List<StaffBean>) util.staff(userCode, null).getData();
                            if (null != staffBeanList && staffBeanList.size() > 0) {
                                userIds.add(staffBeanList.get(0).getUserid());
                            }
                        }
                    }
                }
            } else {
                DeptSafetyStaffBean deptSafetyStaffBean = factory.getMapper(GridNodeMapper.class).deptStaff(gridNode);
                if (null != deptSafetyStaffBean) {
                    if (StringUtils.isEmpty(deptSafetyStaffBean.getSafetyOfficer())) {
                        if (StringUtils.isNotEmpty(deptSafetyStaffBean.getSafetyLeader())) {
                            String[] userCodes = deptSafetyStaffBean.getSafetyLeader().split("[,，]");
                            if (null != userCodes && userCodes.length > 0) {
                                for (String userCode : userCodes) {
                                    List<StaffBean> staffBeanList = (List<StaffBean>) util.staff(userCode, null).getData();
                                    if (null != staffBeanList && staffBeanList.size() > 0) {
                                        userIds.add(staffBeanList.get(0).getUserid());
                                    }
                                }
                            }
//                        } else {
//                            //去找离得最近的网格节点的相关人
//                            setGridManager(userIds, gridNode, audit);
                        }
                    } else {
                        String[] userCodes = deptSafetyStaffBean.getSafetyOfficer().split("[,，]");
                        if (null != userCodes && userCodes.length > 0) {
                            for (String userCode : userCodes) {
                                List<StaffBean> staffBeanList = (List<StaffBean>) util.staff(userCode, null).getData();
                                if (null != staffBeanList && staffBeanList.size() > 0) {
                                    userIds.add(staffBeanList.get(0).getUserid());
                                }
                            }
                        }
                    }
//                } else {
//                    //去找离得最近的网格节点相关人
//                    setGridManager(userIds, gridNode, audit);
                }
            }
            return userIds.stream().collect(Collectors.joining(","));
        }
    }

    //查找当前网格最近的网格负责人
    private void setGridManager(List<String> userIds, String code, Audit audit) {
        Map<String, String> map = nodeService.gridStaff(code);
        if (null != map && !map.isEmpty()) {
            // 第一责任人
            String safetyStaffCode = map.get("safetyStaffCode");
            // 直接责任人
            String businessStaffCode = map.get("businessStaffCode");
            // 安全分管领导
            String safetyLeader = map.get("safetyLeader");
            // 安全员
            String safetyOfficer = map.get("safetyOfficer");
            String gridCode = map.get("code");
            audit.setNextGridNode(gridCode);
            String userCodeStr = null;
            // 流程审批人取安全员，若未设置安全员，则取安全分管领导的值
            if (StringUtils.isNotEmpty(safetyOfficer)) {
                userCodeStr = safetyOfficer;
            } else {
                userCodeStr = safetyLeader;
            }
            if (StringUtils.isNotEmpty(userCodeStr)) {
                String[] userCodes = userCodeStr.split("[,,]");
                if (null != userCodes && userCodes.length > 0) {
                    for (String userCode : userCodes) {
                        List<StaffBean> staffBeanList = (List<StaffBean>) util.staff(userCode, null).getData();
                        if (null != staffBeanList && staffBeanList.size() > 0) {
                            userIds.add(staffBeanList.get(0).getUserid());
                        }
                    }
                }
            }
        }
    }

}
